package com.belazy.auto.configure.service.impl;

import com.aliyun.ocr_api20210707.Client;
import com.aliyun.ocr_api20210707.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.belazy.auto.configure.properties.AliyunOrcProperties;
import com.belazy.auto.configure.service.IOrcService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * 阿里云OCR识别
 *
 * @author tangcp
 */
@Slf4j
public class AliyunOrcService implements IOrcService {
    private Client client;
    private static final String OK = "200";

    public AliyunOrcService(AliyunOrcProperties properties) {
        Config config = new Config ()
                .setAccessKeyId (properties.getAccessKeyId ())
                .setAccessKeySecret (properties.getAccessKeySecret ())
                .setEndpoint (properties.getEndpoint ());
        try {
            client = new Client (config);
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    public static void main(String[] args) {
        AliyunOrcProperties properties = new AliyunOrcProperties ();
        properties.setEndpoint ("ocr-api.cn-hangzhou.aliyuncs.com");
        properties.setAccessKeyId ("LTAI5tSptiBJ3YH8ZkTFRDm5");
        properties.setAccessKeySecret ("tAq4sqqUqwewJ9xXgGcism048yxUTV");
        IOrcService iOrcService = new AliyunOrcService (properties);
        try {
            FileInputStream fileInputStream = new FileInputStream ("");
            String str = iOrcService.recognizeIdCard (fileInputStream);
            log.info ("身份证OCR识别:{}",str);
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        }
    }

    @Override
    public String recognizeIdCard(InputStream body) {
        RecognizeIdcardRequest request = new RecognizeIdcardRequest ();
        request.setBody (body);
        try {
            RecognizeIdcardResponse response = client.recognizeIdcard (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeIdcardResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeBankCard(InputStream body) {
        RecognizeBankCardRequest request = new RecognizeBankCardRequest ();
        request.setBody (body);
        try {
            RecognizeBankCardResponse response = client.recognizeBankCard (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeBankCardResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeBusinessLicense(InputStream body) {
        RecognizeBusinessLicenseRequest request = new RecognizeBusinessLicenseRequest ();
        request.setBody (body);
        try {
            RecognizeBusinessLicenseResponse response = client.recognizeBusinessLicense (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeBusinessLicenseResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeVehicleCertification(InputStream body) {
        RecognizeVehicleCertificationRequest request = new RecognizeVehicleCertificationRequest ();
        request.setBody (body);
        try {
            RecognizeVehicleCertificationResponse response = client.recognizeVehicleCertification (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeVehicleCertificationResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeDrivingLicense(InputStream body) {
        RecognizeDrivingLicenseRequest request = new RecognizeDrivingLicenseRequest ();
        request.setBody (body);
        try {
            RecognizeDrivingLicenseResponse response = client.recognizeDrivingLicense (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeDrivingLicenseResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeVehicleRegistration(InputStream body) {
        RecognizeVehicleRegistrationRequest request = new RecognizeVehicleRegistrationRequest ();
        request.setBody (body);
        try {
            RecognizeVehicleRegistrationResponse response = client.recognizeVehicleRegistration (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeVehicleRegistrationResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeCarVinCode(InputStream body) {
        RecognizeCarVinCodeRequest request = new RecognizeCarVinCodeRequest ();
        request.setBody (body);
        try {
            RecognizeCarVinCodeResponse response = client.recognizeCarVinCode (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeCarVinCodeResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeInvoice(InputStream body) {
        RecognizeInvoiceRequest request = new RecognizeInvoiceRequest ();
        request.setBody (body);
        try {
            RecognizeInvoiceResponse response = client.recognizeInvoice (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeInvoiceResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeCarInvoice(InputStream body) {
        RecognizeCarInvoiceRequest request = new RecognizeCarInvoiceRequest ();
        request.setBody (body);
        try {
            RecognizeCarInvoiceResponse response = client.recognizeCarInvoice (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeCarInvoiceResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

    @Override
    public String recognizeUsedCarInvoice(InputStream body) {
        RecognizeUsedCarInvoiceRequest request = new RecognizeUsedCarInvoiceRequest ();
        request.setBody (body);
        try {
            RecognizeUsedCarInvoiceResponse response = client.recognizeUsedCarInvoice (request);
            if (ObjectUtils.isEmpty (response) || 200 != response.getStatusCode ().intValue ()) {
                log.error ("查询请求失败!");
                return null;
            }
            RecognizeUsedCarInvoiceResponseBody responseBody = response.getBody ();
            if (OK.equals (responseBody.getCode ())) {
                log.info ("识别结果:{}", responseBody);
                return responseBody.getData ();
            } else {
                log.error ("查询RequestId={}请求失败,CODE={},Message={}", responseBody.getRequestId (), responseBody.getCode (), responseBody.getMessage ());
            }
        } catch (Exception e) {
            log.error ("查询请求异常={}", e.getMessage ());
        }
        return null;
    }

}
